﻿@using VinhSon.Intranet.Models
@model VinhSon.Intranet.Models.UserModel
@{
    ViewBag.Title = "Edit User";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section script
{
    <script type="text/javascript">
        function submitForm() {
            if (window.App.FormPanel1.getForm().isValid()) {
                var pass1 = window.App.Password.getValue();
                var pass2 = window.App.ConfirmPassword.getValue();
                if (pass1 == pass2) {
                    var submit = window.App.FormPanel1.getValue();
                    if (submit)
                        if (submit.role) {
                            var roles = submit.role.toString().split(',');
                            if (roles) {
                                for (var j = 0; j < roles.length; j++) {
                                    var s = roles[j].split('_');
                                    if (roles.length == 1)
                                        submit.role = s[1];
                                    else submit.role[j] = s[1];
                                }
                            }
                        }
                    request('@Url.Action("Edit")', submit, function (result) {
                        showNotification(window.parent, result, function () {
                            window.parent.App.gvData.getStore().reload();
                            window.parent.App.winEdit.close();
                        });
                    }, function () {
                        window.Ext.Msg.show(
                            {
                                title: "Confirm",
                                buttons: window.Ext.Msg.YES,
                                fn: function () {
                                    window.parent.App.winEdit.close();
                                },
                                icon: window.Ext.Msg.ERROR,
                                msg: "There some errors occour when update. Please try again later."
                            });
                    });
                } else
                    window.Ext.Msg.show(
                        {
                            title: "Confirm",
                            buttons: window.Ext.Msg.YES,
                            icon: window.Ext.Msg.ERROR,
                            msg: "Password not match."
                        });
            }
        }
    </script>
}
@(Html.X()
     .FormPanel()
     .ID("FormPanel1")
     .Method(HttpMethod.POST)
     .BodyPadding(10)
     .AutoScroll(true)
     .Border(false)
     .Title("Update User")
     .Items(form =>
                {
                    form.Add(Html.X()
                                .TextFieldFor(c => c.Name)
                                .FieldLabel("Name")
                                .AllowBlank(false)
                                .LabelWidth(120)
                                .InputWidth(200)
                                .EmptyText("Name"));
                    form.Add(Html.X()
                                 .TextFieldFor(c => c.Username)
                                 .FieldLabel("Username/Email")
                                 .AllowBlank(false)
                                 .LabelWidth(120)
                                 .InputWidth(200)
                                 .EmptyText("Username/Email"));
                    form.Add(Html.X()
                                 .TextFieldFor(c => c.Password)
                                 .FieldLabel("Password")
                                 .LabelWidth(120)
                                 .InputWidth(200)
                                 .EmptyText("Password"));
                    form.Add(Html.X()
                                 .TextFieldFor(c => c.ConfirmPassword)
                                 .FieldLabel("Confirm Password")
                                 .LabelWidth(120)
                                 .InputWidth(200)
                                 .EmptyText("Confirm Password"));
                    form.Add(Html.X()
                                 .CheckboxFor(c => c.IsActivated)
                                 .LabelWidth(120)
                                 .FieldLabel("Is Activated"));
                    form.Add(Html.X()
                                 .CheckboxFor(c => c.IsLockedOut)
                                 .LabelWidth(120)
                                 .FieldLabel("Is Locked-Out"));
                    form.Add(Html.X()
                                 .TextAreaFor(c => c.Remark)
                                 .FieldLabel("Remark")
                                 .LabelWidth(120)
                                 .InputWidth(200)
                                 .Height(50)
                                 .EmptyText("Remark"));
                    form.Add(Html.X()
                                 .Button()
                                 .FormBind(true)
                                 .Text("Update")
                                 .MarginSpec("0 5 0 120")
                                 .OnClientClick("submitForm"));
                    form.Add(Html.X()
                                 .Button()
                                 .Text("Cancel")
                                 .OnClientClick("window.parent.App.winEdit.close();"));
                }))